window.requestAnimFrame = (function() {
    return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element) {
            return window.setTimeout(callback, 1000 / 60);
        };
})();


var eleContainer = document.getElementById("container");
var bgCanvas = document.getElementById("bgCanvas");
var mainCanvas = document.getElementById("mainCanvas");
var canWidth;
var canHeight;
var container_resize = function(){
    // canWidth = window.innerWidth > 981 ? 981 : window.innerWidth;
    canWidth = window.innerWidth < 500 ? 500 : window.innerWidth;
    canHeight = window.innerHeight - document.getElementsByTagName("header")[0].offsetHeight - document.getElementsByTagName("footer")[0].offsetHeight;
    eleContainer.style.width = canWidth + "px";
    eleContainer.style.height = canHeight + "px";

    bgCanvas.width = canWidth;
    bgCanvas.height = canHeight;

    mainCanvas.width = canWidth;
    mainCanvas.height = canHeight;
};
container_resize();
window.onresize = container_resize;

var ctx = bgCanvas.getContext("2d");
var ctxMain = mainCanvas.getContext("2d");
//utils.drawCloud(ctx, 0, 0, 60);
//utils.drawCloud(ctx, 600, 0, 100);
//utils.drawCloud(ctx, 200, 100, 10);
//utils.drawBalloon(ctxMain, 500, 300, 50, "#ffcc01", "#e39006", -30);
//utils.drawBalloon(ctxMain, 200, -140, 50, "#01ce35", "#006d1e", -15);
//画曲线 sin曲线
//utils.curveSin(ctx, 100, 200, 600, 100);

//云
var cloud = new cloudObj();
cloud.init();

//气球
var balloon = new balloonObj();
balloon.init();

//气球爆炸碎片
var debris = new debrisObj();
debris.init();

//红色爱心
var heart = new heartObj();
heart.init();

//分数
var data = new dataObj();

var lastTime = Date.now();
var deltaTime = 0;
function gameLoop(){
    requestAnimFrame(gameLoop);
    var now = Date.now();
    deltaTime = now-lastTime;
    lastTime = now;
    if(deltaTime>40) deltaTime = 40;
    ctxMain.clearRect(0, 0, canWidth, canHeight);
    ctx.clearRect(0, 0, canWidth, canHeight);

    cloud.draw();

    balloonMonitor();
    balloon.draw();

    debris.draw();

    heart.draw();

    data.draw();
}
document.body.onload = gameLoop;

var ua = navigator.userAgent;
var iPad = ua.match(/(iPad).*OS\s([\d_]+)/),
    isIphone = !iPad && ua.match(/(iPhone\sOS)\s([\d_]+)/),
    isAndroid = ua.match(/(Android)\s+([\d.]+)/),
    isMobile = isIphone || isAndroid;
if(isMobile) {
    document.addEventListener('touchstart', function(e) {
        e.preventDefault();
        doClick(e.pageX , e.pageY);
        console.log(e)

    }, false);
}else {
    eleContainer.onclick = function(e){
        doClick(e.offsetX, e.offsetY)
    };
}
function doClick(x, y){
    if(!data.gameOver){
        for(var i = balloon.num - 1; i > -1; i--){
            var betweenX = x - balloon.x[i];
            var betweenY = y - balloon.y[i];
            if((betweenX*betweenX + betweenY*betweenY) <= balloon.r[i]*balloon.r[i]){
                data.addScore();
                balloon.boom(i);
            }
        }
    }
}
